package com.idanchuang.count.support;

import com.idanchuang.component.logback.metric.MetricLogger;
import com.idanchuang.count.core.constant.UserCountEventTypeEnum;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Richard_yyf
 * @version 1.0 2021/5/28
 */
public class MetricCollector {

    /**
     * METRIC
     * count_async_event_sent - 异步消息发送数
     * count_async_event_sent_fail - 异步消息发送失败数
     * count_async_event_consume_time - 异步消息消费耗时
     * count_async_event_consumed - 异步消息消费数
     */
    private static final String METRIC_ASYNC_EVENT_SENT = "count_async_event_sent";
    private static final String METRIC_ASYNC_EVENT_SENT_FAIL = "count_async_event_sent_fail";
    private static final String METRIC_ASYNC_EVENT_CONSUME_TIME = "count_async_event_consume_time";
    private static final String METRIC_ASYNC_EVENT_CONSUMED = "count_async_event_consumed";

    /**
     * TAG
     * event_type - event类型
     */
    private static final String TAG_EVENT_TYPE = "event_type";

    public static void collectAfterEventSent(String eventTypeTag) {
        Map<String, Object> meta = buildMeta(eventTypeTag);
        MetricLogger.log(METRIC_ASYNC_EVENT_SENT, 1, meta);
    }

    private static Map<String, Object> buildMeta(String eventTypeTag) {
        Map<String, Object> meta = new HashMap<>(2);
        meta.put(TAG_EVENT_TYPE, eventTypeTag);
        return meta;
    }

    public static void collectAfterEventSentFail(String eventTypeTag) {
        Map<String, Object> meta = buildMeta(eventTypeTag);
        MetricLogger.log(METRIC_ASYNC_EVENT_SENT_FAIL, 1, meta);
    }

    public static void collectAfterEventConsumed(String eventTypeTag, int size, long start) {
        Map<String, Object> meta = buildMeta(eventTypeTag);
        MetricLogger.log(METRIC_ASYNC_EVENT_CONSUMED, size, meta);
        MetricLogger.log(METRIC_ASYNC_EVENT_CONSUME_TIME, System.currentTimeMillis() - start, meta);
    }
}
